/*
 * ZBrowser is an embeddable browser component.
 * Copyright (C) Author: Gangadhar Nagesh Metla (Novell, Inc.)
 * nmetla74@gmail.com 
 * Version 1.0
 * 1/3/2009
 * Filename PropPageCmdDialog.java
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.novell.zenworks.agent.zbrowser.cmddialogs;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.text.BreakIterator;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import com.novell.zenworks.agent.zbrowser.ZBrowser;

/*
 * BusydialogView.java
 */

/**
 * The application's main frame.
 */
public class PropPageCmdDialog extends JDialog implements KeyListener, ActionListener
{
    /**
     * 
     */
    private static final long serialVersionUID = 4592091738075428391L;
    private JFrame frame;
    private String message;
    private String title;
    private String type;
    private String detailMessage;
    private boolean modal;
    public static final String BUSY = "busy";
    public static final String SUCCESS = "success";
    public static final String ERROR = "error";
    private static final String busyImage = ZBrowser.getApplicationResourcePath() + File.separator + "busyanimation.gif";
    private static final String successImage = ZBrowser.getApplicationResourcePath() + File.separator + "Complete.gif";
    private static final String errorImage = ZBrowser.getApplicationResourcePath() + File.separator + "Error.gif";
    
    private JButton button;
    private JLabel caption;
    private JPanel captionPanel;
    private JPanel buttonPanel;
    private JPanel detailMessagePanel;
    private JScrollPane mainScrollPanel;
    private JPanel mainPanel;
    private JScrollPane detailMessageScrollPanel;
	private JTextArea detailMessageTextArea;
    
    public PropPageCmdDialog(JFrame frame, String message, String title, String type, String detailMessage, boolean modal)
    {
        super(frame);
        this.frame = frame;
        this.message = message;
        this.title = title;
        this.type = type;
        this.detailMessage = detailMessage;
        this.modal = modal;
        this.setTitle(title); 
        
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        initComponents();
        if(type.equalsIgnoreCase(BUSY))
        {
            hideErrorTextArea(); 
            hideButton();
        }
        else if(type.equalsIgnoreCase(SUCCESS))
        {
            hideErrorTextArea();
        }
        
        setSize(new Dimension(425,200));
        setPreferredSize(new Dimension(425,190));
        pack();
        setModal(modal);
        setEnabled(true);
    }

    public void hideButton()
    {
       button.setVisible(false);        
    }

    private void initComponents() {
    	java.awt.GridBagConstraints gridBagConstraints;
    	Font bfont = new Font("Trebuchet MS", Font.BOLD, 12);
    	Font tfont = new Font("Trebuchet MS", Font.PLAIN, 12);
    	Font font = new Font("Trebuchet MS", Font.PLAIN, 11);
        mainScrollPanel = new JScrollPane();
        mainPanel = new JPanel();
        detailMessagePanel = new JPanel();
        detailMessageScrollPanel = new JScrollPane();
        detailMessageTextArea = new JTextArea();
        captionPanel = new JPanel();
        caption = new JLabel();
        buttonPanel = new JPanel();
        button = new JButton();

        mainScrollPanel.setBackground(new Color(236,236,236)); // NOI18N
        mainScrollPanel.setAutoscrolls(true);
        mainScrollPanel.setMinimumSize(new java.awt.Dimension(400, 150));
        mainScrollPanel.setName("mainScrollPanel"); // NOI18N

        mainPanel.setBackground(new Color(236,236,236)); // NOI18N
        mainPanel.setAutoscrolls(true);
        mainPanel.setMinimumSize(new java.awt.Dimension(400, 150));
        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setLayout(new java.awt.GridBagLayout());
        mainScrollPanel.add(mainPanel);
        mainScrollPanel.setViewportView(mainPanel);
        
        //captionPanel.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        captionPanel.setBackground(new Color(236,236,236)); // NOI18N
        captionPanel.setMaximumSize(new java.awt.Dimension(400, 50));
        captionPanel.setMinimumSize(new java.awt.Dimension(400, 50));
        captionPanel.setName("captionPanel"); // NOI18N
        captionPanel.setLayout(new BoxLayout(captionPanel, BoxLayout.LINE_AXIS));
        
        caption.setBackground(new Color(236,236,236)); // NOI18N
        caption.setFont(bfont); // NOI18N
        if(type.equalsIgnoreCase(BUSY))
            caption.setIcon(new ImageIcon(busyImage)); // NOI18N
        else if(type.equalsIgnoreCase(ERROR))
            caption.setIcon(new ImageIcon(errorImage)); // NOI18N
        else
            caption.setIcon(new ImageIcon(successImage)); // NOI18N        
        wrapMessageText(caption, message, 370);
        caption.setAutoscrolls(true);
        caption.setMaximumSize(new java.awt.Dimension(400, 50));
        caption.setMinimumSize(new java.awt.Dimension(400, 50));
        caption.setName("caption"); // NOI18N
        caption.setOpaque(true);
        caption.setPreferredSize(new java.awt.Dimension(400, 50));
        captionPanel.add(caption);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        mainPanel.add(captionPanel, gridBagConstraints);
        
        detailMessageScrollPanel.setAutoscrolls(true);
        detailMessageScrollPanel.setMinimumSize(new java.awt.Dimension(400, 55));
        detailMessageScrollPanel.setName("detailMessageScrollPanel"); // NOI18N
        detailMessageScrollPanel.setOpaque(false);
        detailMessageScrollPanel.setPreferredSize(new java.awt.Dimension(400, 55));
        
        detailMessageTextArea.setColumns(30);
        detailMessageTextArea.setRows(5);
        detailMessageTextArea.setLineWrap(true);
        detailMessageTextArea.setWrapStyleWord(true);
        detailMessageTextArea.setMinimumSize(new java.awt.Dimension(380, 55));
        detailMessageTextArea.setName("detailMessageTextArea"); // NOI18N
        detailMessageTextArea.setText(detailMessage);
        detailMessageTextArea.setFont(tfont);
        detailMessageTextArea.setPreferredSize(new java.awt.Dimension(380, 55));
        detailMessageTextArea.setAutoscrolls(true);
        detailMessageTextArea.setEditable(false);
        detailMessageScrollPanel.add(detailMessageTextArea);
        detailMessageScrollPanel.setViewportView(detailMessageTextArea);

        //detailMessagePanel.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        detailMessagePanel.setBackground(new Color(236,236,236)); // NOI18N
        detailMessagePanel.setMinimumSize(new java.awt.Dimension(400, 55));
        detailMessagePanel.setName("detailMessagePanel"); // NOI18N
        detailMessagePanel.setPreferredSize(new java.awt.Dimension(400, 55));
        detailMessagePanel.setLayout(new java.awt.GridLayout(1, 0, 5, 5));        
        detailMessagePanel.add(detailMessageScrollPanel);        

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 5;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        mainPanel.add(detailMessagePanel, gridBagConstraints);        

        //buttonPanel.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        buttonPanel.setBackground(new Color(236,236,236)); // NOI18N
        buttonPanel.setFont(font); // NOI18N
        buttonPanel.setMinimumSize(new java.awt.Dimension(400, 30));
        buttonPanel.setName("buttonPanel"); // NOI18N
        buttonPanel.setPreferredSize(new java.awt.Dimension(400, 30));
        buttonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));

        button.setFont(tfont); // NOI18N
        button.setText(ZBrowser.rm.getString("CloseButtonText")); // TODO NOI18N
        button.setMinimumSize(new java.awt.Dimension(75, 25));
        button.setName("button"); // NOI18N
        button.addActionListener(this);
        button.addKeyListener(this);
        buttonPanel.add(button);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        mainPanel.add(buttonPanel, gridBagConstraints);       

        this.getContentPane().add(mainScrollPanel);
    }
    
    public void hideErrorTextArea()
    {
        detailMessagePanel.setVisible(false);
    }
    
    public void showErrorDetailMessage(String message)
    {
        detailMessageTextArea.setText(message);
        detailMessagePanel.setVisible(true);
        showButton();
    }
    
    public void updateCaption(String captionMsg, String type)
    {
        if(type.equalsIgnoreCase(SUCCESS))
        {
            caption.setIcon(new ImageIcon(successImage));
        }
        else if(type.equalsIgnoreCase(ERROR))
        {
            caption.setIcon(new ImageIcon(errorImage));
        }
        else
        {
            caption.setIcon(new ImageIcon(busyImage));
        }
        wrapMessageText(caption, captionMsg, 370);
    }
    
    public void showButton()
    {
    	button.requestFocusInWindow();
        button.setVisible(true);
    }
    
    /**
     * @method Method to auto wrap message text.
     * 
     * @param label message to wrap text for.
     * @param width width for wrap text.
     */
    private void wrapMessageText(JLabel label, String message, int width)
    {
        FontMetrics fm = label.getFontMetrics(label.getFont());
        BreakIterator wall = BreakIterator.getWordInstance();
        wall.setText(message);

        StringBuffer poker = new StringBuffer();
        StringBuffer result = new StringBuffer("<html>");

        int start = wall.first();
        for (int end = wall.next(); end != BreakIterator.DONE; start = end, end = wall.next())
        {
            String word = message.substring(start, end);
            poker.append(word);
            int trialWidth = SwingUtilities.computeStringWidth(fm, poker.toString());
            if (trialWidth > width)
            {
                poker = new StringBuffer(word);
                result.append("<br>");
            }
            result.append(word);
        }

        result.append("</html>");
        label.setText(result.toString());
    }

    public void keyPressed(KeyEvent event)
    {
        if(event.getKeyCode() == KeyEvent.VK_ENTER)
        {
            this.dispose();
        }        
    }

    public void keyReleased(KeyEvent e)
    {
      //Dummy Interface Realization for keyListener.        
    }

    public void keyTyped(KeyEvent e)
    {
      //Dummy Interface Realization for keyListener.        
    }

    public void actionPerformed(ActionEvent e)
    {
        this.dispose();
    }
}
